<template>
  <el-button type="text" @click.stop="donload">{{ title }}</el-button>
</template>
<script>
/**
 * @author zhaojiuyi
 * @date 2022/9/25-16:26
 * @version 1.0.0
 */
import JsPDF from "jspdf"
import 'jspdf-autotable'
import '@/assets/js/chs_wk-normal'
import api from "@/apis/api";

export default {
  props: ['title', 'type', 'medicalId'],
  data() {
    return {
      columns: [],
      tableHead: ["title", "value"]
    }
  },
  methods: {
    async donload() {
      const result = await api.getMediaInfo(this.medicalId)
      if (result.code !== 200) {
        this.$util.errorMessage(result.msg)
        return null
      }
      const {data} = result
      Object.keys(data).forEach(key => {
        this.columns.push([key, data[key]])
      })
      const PDF = new JsPDF('', 'pt', 'a4')
      /*设置中文字体，解决乱码问题*/
      PDF.setFont('chs_wk')  //设置自定义字体
      PDF.addImage("/static/medical.jpg","jpg",10,10,575,820)
      PDF.addPage('', 'pt')
      PDF.autoTable({
        styles: {
          font: 'chs_wk', fontStyle: 'normal', textColor: [0, 0, 0], halign: 'center'
        }, // 表格样式
        startY: 40, // 上边距
        body: this.columns, columns: this.tableHead
      });
      //设置pdf的描述信息（标题、作者等）
      PDF.setProperties({
        title: `${data.name}个人病历`,
        subject: 'This is the subject',
        author: '太阳当空丶赵',
        keywords: 'generated, javascript, web 2.0, ajax',
      });
      PDF.save(`${data.name}个人病历.pdf`);
    }
  }

}
</script>
